package org.zcl.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.zcl.entity.TbUserInfo;
import org.zcl.enums.CacheKeyEnum;
import org.zcl.model.CacheKey;
import org.zcl.service.TbUserInfoService;
import org.zcl.utils.CacheUtil;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Package org.zcl.controller
 * @PROJECT_NAME: question
 * @DESCRIPTION:
 * @USER: zhuchenglin
 * @DATE: 2021/12/24 14:14
 */
@RestController
@RequestMapping(value = "redis")
@Api(value = "Redis接口", tags = "RedisController", description = "Redis接口")
public class RedisController {

    @Resource
    private TbUserInfoService tbUserInfoService;

    @Autowired
    private CacheUtil cacheUtil;

    @GetMapping(value = "/getAll", produces = "application/json")
    @ApiOperation(value = "获取用户列表", notes = "获取用户列表")
    public List<TbUserInfo> getAll() {
        List<TbUserInfo> tbUserInfos = cacheUtil.get(CacheKey.getKey(CacheKeyEnum.MAIN_DATA_CHANNELS));
        if (CollectionUtils.isEmpty(tbUserInfos)) {
            tbUserInfos = tbUserInfoService.list(new LambdaQueryWrapper<>());
            cacheUtil.set(CacheKey.getKey(CacheKeyEnum.MAIN_DATA_CHANNELS), tbUserInfos,5, TimeUnit.MINUTES);
        }
        return tbUserInfos;
    }
}
